﻿namespace Tszy.Unlimited.Base.Models;

/// <summary>
/// 响应结果。
/// </summary>
public class ResponseResult
{
    private string _message = string.Empty;

    /// <summary>
    /// 初始化响应结果。
    /// </summary>
    /// <param name="status">验证状态。</param>
    /// <param name="code">返回码。</param>
    /// <param name="message">信息。</param>
    public ResponseResult(bool status, int code, string message)
    {
        Status = status;
        Code = code;
        Message = message ?? string.Empty;
    }

    /// <summary>
    /// 验证状态。
    /// </summary>
    public bool Status { get; set; }

    /// <summary>
    /// 返回码。
    /// </summary>
    public int Code { get; set; }

    /// <summary>
    /// 错误信息。
    /// </summary>
    public string Message
    {

        get
        {
            if (string.IsNullOrWhiteSpace(_message))
                return string.Empty;

            return _message;
        }
        set
        {
            _message = value ?? string.Empty;
        }
    }

    /// <summary>
    /// 生成成功信息。
    /// </summary>
    /// <returns>响应结果。</returns>
    public static ResponseResult Success()
    {
        return new ResponseResult(true, (int)ModelResultCode.Success, string.Empty);
    }

    /// <summary>
    /// 生成成功信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <returns>响应结果。</returns>
    public static ResponseResult Success(string message)
    {
        return new ResponseResult(true, (int)ModelResultCode.Success, message);
    }

    /// <summary>
    /// 生成失败信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <returns>响应结果。</returns>
    public static ResponseResult Error(string message)
    {
        return new ResponseResult(false, 0, message);
    }

    /// <summary>
    /// 生成失败信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <param name="code">返回码。</param>
    /// <returns>响应结果。</returns>
    public static ResponseResult Error(string message, int code)
    {
        return new ResponseResult(false, code, message);
    }
}

/// <summary>
/// 响应结果。
/// </summary>
public class ResponseResult<T> : ResponseResult
{
    /// <summary>
    /// 初始化响应结果。
    /// </summary>
    /// <param name="status">验证状态。</param>
    /// <param name="code">返回码。</param>
    /// <param name="message">信息。</param>
    /// <param name="data">结果数据。</param>
    public ResponseResult(bool status, int code, string message, T? data) : base(status, code, message)
    {
        Data = data;
    }

    /// <summary>
    /// 数据
    /// </summary>
    public T? Data { get; set; }

    /// <summary>
    /// 生成成功信息。
    /// </summary>
    /// <returns>响应结果。</returns>
    public new static ResponseResult<T> Success()
    {
        return new ResponseResult<T>(true, (int)ModelResultCode.Success, string.Empty, default);
    }

    /// <summary>
    /// 生成成功信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <returns>响应结果。</returns>
    public new static ResponseResult<T> Success(string message)
    {
        return new ResponseResult<T>(true, (int)ModelResultCode.Success, message, default);
    }

    /// <summary>
    /// 生成成功信息。
    /// </summary>
    /// <param name="data">结果数据。</param>
    /// <returns>响应结果。</returns>
    public static ResponseResult<T> Success(T data)
    {
        return new ResponseResult<T>(true, (int)ModelResultCode.Success, string.Empty, data);
    }

    /// <summary>
    /// 生成成功信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <param name="data">结果数据。</param>
    /// <returns>响应结果。</returns>
    public static ResponseResult<T> Success(string message, T data)
    {
        return new ResponseResult<T>(true, (int)ModelResultCode.Success, message, data);
    }

    /// <summary>
    /// 生成失败信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <returns>响应结果。</returns>
    public new static ResponseResult<T> Error(string message)
    {
        return new ResponseResult<T>(false, 0, message, default);
    }

    /// <summary>
    /// 生成失败信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <param name="code">返回码。</param>
    /// <returns>响应结果。</returns>
    public new static ResponseResult<T> Error(string message, int code)
    {
        return new ResponseResult<T>(false, code, message, default);
    }

    /// <summary>
    /// 生成失败信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <param name="data">结果数据。</param>
    /// <returns>响应结果。</returns>
    public static ResponseResult<T> Error(string message, T data)
    {
        return new ResponseResult<T>(false, 0, message, data);
    }

    /// <summary>
    /// 生成失败信息。
    /// </summary>
    /// <param name="message">信息。</param>
    /// <param name="data">结果数据。</param>
    /// <param name="code">返回码。</param>
    /// <returns>响应结果。</returns>
    public static ResponseResult<T> Error(string message, T data, int code)
    {
        return new ResponseResult<T>(false, code, message, data);
    }
}